﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using NPOI.SS.UserModel;
using System.Data.EntityClient;

namespace HRWEBExport
{
    public static class ModelExtensions
    {
        public static string FindHeadshipType(this THeadShip headship)
        {
            string typeString = headship.FHeadShipCode.Substring(0, 2);
            hrdbEntities ent = new hrdbEntities();
            var scale = from t in ent.TDictHeadShips where t.FHeadShipCode == typeString select t.FName;
            if (scale.Any())
            {
                return scale.Single();
            }
            else
            {
                return "未知";
            }
        }
        public static string IsSubmitted(this TEmployee emp)
        {
            return emp.TInfoSummaries.Any() ? (emp.TInfoSummaries.OrderByDescending(p => p.SerialNO).FirstOrDefault().FullyFilled ? "是" : "否") : "否";
        }
        public static string Feedback(this TEmployee emp)
        {
            return emp.TInfoSummaries.Any() ? emp.TInfoSummaries.OrderByDescending(p => p.SerialNO).FirstOrDefault().Feedback : "";
        }
        public static string GetKQDeptCode(this TEmployee emp)
        {
            return emp.TDepts.OrderBy(x => x.FValidDate).Last().FKQDeptCode;
        }
        public static string GetKQDeptName(this TEmployee emp)
        {
            string deptCode = emp.TDepts.OrderByDescending(x => x.FValidDate).First().FKQDeptCode;
            EntityConnection conn = new EntityConnection(Form1.GetConnectionString());
            using (hrdbEntities ent = new hrdbEntities(conn,true))
            {
                return ent.TDictDepts.Where(p => p.FDeptCode == deptCode).Select(p => p.FName).First();
            }
        }
        public static string GetBelongOrgan(this TEmployee emp)
        {

            if (emp.TDictBelongOrgan == null)
            {
                return "";
            }
            else
            {
                return emp.TDictBelongOrgan.FName;
            }
            
        }
        public static int GetAge(this TEmployee emp)
        {
            if (emp.FBirthday.HasValue) return DateTime.Now.Year - emp.FBirthday.Value.Year; else return 0;
        }
        public static int GetWorkedYears(this TEmployee emp)
        {
            return emp.FFirstWorkDate.HasValue ? DateTime.Now.Year - emp.FFirstWorkDate.Value.Year : 0;
        }
        public static int GetIntoHisYears(this TEmployee emp)
        {
            return emp.FIntoHisDate.HasValue ? DateTime.Now.Year - emp.FIntoHisDate.Value.Year : 0;
        }
        public static void Clone(this TInfoSummary s1, TInfoSummary s2)
        {
            s1.FEmployeeID = s2.FEmployeeID;
            s1.Feedback = s2.Feedback;
            s1.FullyFilled = s2.FullyFilled;
        }
        public static void SetCellBorder(this IRow row)
        {
            foreach (ICell cell in row.Cells)
            {
                cell.CellStyle.BorderTop = BorderStyle.THIN;
                cell.CellStyle.BorderRight = BorderStyle.THIN;
                cell.CellStyle.BorderBottom = BorderStyle.THIN;
                cell.CellStyle.BorderLeft = BorderStyle.THIN;
            }
        } 
        
        public static bool Validate(this THouseHold hh)
        {
            bool t = hh.FBirthday != null && !string.IsNullOrEmpty(hh.FCompany) && !string.IsNullOrEmpty(hh.FHeadship) && !string.IsNullOrEmpty(hh.FName);
            return t;
        }
        public static bool Validate(this TReward re)
        {
            bool t = !string.IsNullOrEmpty(re.Name) && !string.IsNullOrEmpty(re.ApprovedOrgan) && re.ValidDate != null;
            return t;
        }
        public static bool Validate(this TEmployee emp)
        {
            bool t = string.IsNullOrEmpty(emp.FBornCity) || string.IsNullOrEmpty(emp.FMobileTel) || string.IsNullOrEmpty(emp.FAdress) || !emp.THouseHolds.Any();
            return !t;
        }
        
    }
}